package com.haierCamera.customapplication.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Toast;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.databinding.HzklActivitySerialNextBinding;
import com.haierCamera.customapplication.ui.business.Business;
import com.haierCamera.customapplication.ui.zxing.activity.CaptureActivity;
import com.lechange.opensdk.api.bean.CheckDeviceBindOrNot;

import androidx.databinding.DataBindingUtil;

public class CameraSerialNextActivity extends BaseActivity {

    HzklActivitySerialNextBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_serial_next);
        binding.imgReturn.setOnClickListener((v)->{
            finish();
        });

        binding.btnNext.setOnClickListener((v)->{
            checkOnBindAndRemind(binding.edSn.getText().toString()
                    ,binding.edSc.getText().toString());
        });

        binding.edSn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!editable.toString().equals("")&& !binding.edSc.getText().toString().equals("")){
                    binding.btnNext.setBackgroundDrawable(getResources().getDrawable(R.drawable.hzkl_btn_blue));
                    binding.btnNext.setClickable(true);
                } else {
                    binding.btnNext.setBackgroundDrawable(getResources().getDrawable(R.drawable.hzkl_btn_grey));
                    binding.btnNext.setClickable(false);
                }
            }
        });

        binding.edSc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!editable.toString().equals("")&& !binding.edSn.getText().toString().equals("")){
                    binding.btnNext.setBackgroundDrawable(getResources().getDrawable(R.drawable.hzkl_btn_blue));
                    binding.btnNext.setClickable(true);
                } else {
                    binding.btnNext.setBackgroundDrawable(getResources().getDrawable(R.drawable.hzkl_btn_grey));
                    binding.btnNext.setClickable(false);
                }
            }
        });
    }

    public void checkOnBindAndRemind(String sn,String sc) {
        Business.getInstance().checkBindOrNot(sn,
                new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        // TODO Auto-generated method stub
                        super.handleMessage(msg);
                        Business.RetObject retObject = (Business.RetObject) msg.obj;
                        if (msg.what == 0) {
                            CheckDeviceBindOrNot.Response resp = (CheckDeviceBindOrNot.Response) retObject.resp;
                            if (!resp.data.isBind) {
                                startActivity(new Intent(CameraSerialNextActivity.this, HZKLCameraDeviceAddStepOneActivity.class)
                                        .putExtra("sn", sn)
                                        .putExtra("sc", sc)
                                        .putExtra("dt", binding.edDt.getText().toString()));
                                finish();
                            } else if (resp.data.isBind && resp.data.isMine)
//                                toast(getString(R.string.HZKLtoast_adddevice_already_binded_by_self));
                                Toast.makeText(CameraSerialNextActivity.this, "" + getString(R.string.HZKLtoast_adddevice_already_binded_by_self)
                                        , Toast.LENGTH_SHORT).show();
                            else
//                                toast(getString(R.string.HZKLtoast_adddevice_already_binded_by_others));
                                Toast.makeText(CameraSerialNextActivity.this, "" + getString(R.string.HZKLtoast_adddevice_already_binded_by_others)
                                        , Toast.LENGTH_SHORT).show();
                        } else {
                            Log.i("TAG","checkOnBindAndRemind-" + retObject.mMsg);
//                            toast(retObject.mMsg);
                            Toast.makeText(CameraSerialNextActivity.this, "" + retObject.mMsg
                                    , Toast.LENGTH_SHORT).show();
                        }

                    }
                });
    }
}
